package com.mybatis.demo.plugin;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyProxy4 {

  public static void main(String[] args) {
    User user =new UserImpl();
    ProxyFactory factory = new ProxyFactory(user);
     user = (User)factory.getProxy();
     user = (User)factory.getProxy();
     user.hello();
  }


  static class ProxyFactory implements InvocationHandler {
    private Object target;

    public ProxyFactory(Object target) {
      this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      System.out.println("前置。。。。。。。。。。。。。。。。。。。。。。。");
      return method.invoke(target,args);
    }

    public  Object getProxy(){
      return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),this);
    }
  }

  interface User{
    void hello();
  }

  static class UserImpl implements User{

    @Override
    public void hello() {
      System.out.println("呵呵呵。。。。。。。。。。。。。。。");
    }
  }
}
